package com.srini.util;

/**
 * @author srinivasarao_vassadi
 */
import java.util.Random;

public class Encryptor {

	/*
	 * public static final int SUCCESS = 0; public static final int FAIL_LENGTH
	 * = 1; public static final int FAIL_SAME_AS_OLD = 2; public static final
	 * int FAIL_SAME_AS_USERNAME = 3; public static final int FAIL_IN_PATTERN =
	 * 4; public static final int FAIL_SAME_SESSION = 5; public static final int
	 * FAIL_WRONG_OLD_PASSWORD = 6; public static final int FAIL_INVALID_SESSION
	 * = 7;
	 */

	/**
	 * Generate random alpha numeric password.
	 * 
	 * @param loginName
	 *            the loginName of a user.
	 * @return Returns the generated password.
	 */
	public static String generatePassword() {
		// Define a Constants alpha-numeric String
		final String UPPER_CHAR_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		final String LOWER_CHAR_STRING = "abcdefghijklmnopqrstuvwxyz";
		final String DIGITS_STRING = "0123456789";

		// Generate password of length 6
		// final int PASSWORD_LENGTH = 6;

		Random random = new Random();
		StringBuffer passwordBuff = new StringBuffer();
		// This password must satisfy the following criteria:New Password must
		// include at least one
		// Upper Case, Lower Case letter and a Number. It must not include
		// Space.
		for (int i = 0; i < 2; i++) {
			// Generate a random number from 0(inclusive) to lenght of
			// CHAR_STRING(exclusive).
			// Get the character corrosponding to random number and append it to
			// password buffer.
			int randomVal = random.nextInt(UPPER_CHAR_STRING.length());
			passwordBuff.append(UPPER_CHAR_STRING.charAt(randomVal));
			randomVal = random.nextInt(LOWER_CHAR_STRING.length());
			passwordBuff.append(LOWER_CHAR_STRING.charAt(randomVal));
			randomVal = random.nextInt(DIGITS_STRING.length());
			passwordBuff.append(DIGITS_STRING.charAt(randomVal));
		}
		return passwordBuff.toString();
	}

	public static String encode(String input) {

		char char_O = 'O';
		char char_F = 'f';
		char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		char ch1 = 'A';
		String key = new String("" + ch1);
		key += "WelcomeTocaTISSUECORE" + char_O;
		String in = "";
		key += char_F;
		key += "ThisIsTheFirstReleaseOfWalletDevelopedBySrini";
		for (int i = 0; i < input.length(); i++) {
			in += input.substring(i, i + 1);
			in += key.substring(i, i + 1);
		}

		try {
			byte[] bytes = in.getBytes();
			StringBuffer s = new StringBuffer(bytes.length * 2);

			for (byte b : bytes) {
				s.append(digits[(b & 0xf0) >> 4]);
				s.append(digits[b & 0x0f]);
			}

			return s.toString();
		} catch (Exception e) {
			System.out.println(e);
		}
		return null;
	}

	public static String decode(String s) {
		try {
			int len = s.length();
			byte[] r = new byte[len / 2];
			for (int i = 0; i < r.length; i++) {
				int digit1 = s.charAt(i * 2);
				int digit2 = s.charAt(i * 2 + 1);
				if ((digit1 >= '0') && (digit1 <= '9')) {
					digit1 -= '0';
				} else if ((digit1 >= 'a') && (digit1 <= 'f')) {
					digit1 -= 'a' - 10;
				}
				if ((digit2 >= '0') && (digit2 <= '9')) {
					digit2 -= '0';
				} else if ((digit2 >= 'a') && (digit2 <= 'f')) {
					digit2 -= 'a' - 10;
				}
				r[i] = (byte) ((digit1 << 4) + digit2);
			}
			String sin = new String(r);
			String sout = "";
			for (int i = 0; i < sin.length(); i += 2) {
				sout += sin.substring(i, i + 1);
			}
			return sout;
		} catch (Exception e) {
			System.out.println(e);
		}
		return null;
	}
}